﻿using System;
using System.IO;
using Microsoft.Build.BuildEngine;
using MSBProject = Microsoft.Build.BuildEngine.Project;

 namespace Needle.DevToolkit.Workspace
{
    internal class MSBuildProject : Project, IMSBuildProject
    {
        public MSBProject LoadProject()
        {
            try
            {
                string path = ResolvePath();
                return LoadProject(File.OpenText(path)) ;
            }
            catch (Exception ex)
            {
                throw new FileLoadException("Project file could not be loaded.", ex);
            }
        }
        internal MSBProject LoadProject(TextReader reader)
        {
            MSBProject prj = new MSBProject();
            prj.Load(reader, ProjectLoadSettings.IgnoreMissingImports);
            return prj; 
        }
        private string ResolvePath()
        {
            FileInfo path = new FileInfo(Path);
            if (path.Exists)
                return path.FullName;
            else 
                throw new  FileNotFoundException("File not found", path.FullName);
        }
    }
}
